
//桌面顶部任务栏高度
const PANEL_TOP_SIZE = 30;

//桌面图标的宽度
const APP_ICON_WIDTH = 100;
//桌面图标的高度
const APP_ICON_HEIGH = 120;
//桌面图标的水平间隔
const ICON_H_INTERVAL = 10;
//桌面图标的垂直间隔
const ICON_V_INTERVAL = 20;

const ICON_GRID_WIDTH = 110;
const ICON_GRID_HEIGH = 140;


//坐标从左到右，从上到下，起始为0
class DesktopGrid{
  constructor(){
    this.width = 1880;
    this.height = 840;
    this.sizex = 17;
    this.sizey = 6;
  }

  //根据app的网格坐标，计算页面坐标
  place = function(app){
    let coordinate = app.coordinate;
    let lx = coordinate.x * APP_ICON_WIDTH + ICON_H_INTERVAL;
    let ly = coordinate.y * APP_ICON_HEIGH + ICON_V_INTERVAL + PANEL_TOP_SIZE;
    app.locate(lx, ly);
  };

  //当拖动图标时，需要重新计算app的网格坐标和页面坐标
  relocate = function (app, x, y) {
    let lx = Math.floor(x / ICON_GRID_WIDTH);
    let ly = Math.floor(y / ICON_GRID_HEIGH);
    app.moveto(lx, ly);
    this.place(app);
  };

  area_select = function (applications, area) {
    let chosen = [];
    for(let app of applications){
      let boundary = this.app_boundary(app);
      if(boundary.x0 > area.x0 && boundary.y0 > area.y0 && boundary.x1 < area.x1 && boundary.y1 < area.y1){
        app.choosed();
        chosen.push(app);
      }
    }
    return chosen;
  };

  app_boundary = function (app) {
    let location = app.location;
    let x0 = location.x + 10;
    let y0 = location.y + 20;
    let x1 = location.x + 80;
    let y1 = location.y + 100;
    return {x0, y0, x1 , y1};
  }
}

export {DesktopGrid}
